package org.springblade.modules.system.wrapper;

import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.system.entity.City;
import org.springblade.modules.system.service.ICityService;
import org.springblade.modules.system.vo.CityVO;

import java.util.List;
import java.util.Objects;

public class CityWrapper extends BaseEntityWrapper<City, CityVO> {
	private static ICityService cityService;

	static {
		cityService = SpringUtil.getBean(ICityService.class);
	}

	public static CityWrapper build() {
		return new CityWrapper();
	}

	@Override
	public CityVO entityVO(City city) {
		CityVO cityVO = Objects.requireNonNull(BeanUtil.copy(city, CityVO.class));
		if (city.getParentId() <= 0L) {
			cityVO.setParentName("");
		} else {
			City parentCity = cityService.getById(city.getParentId());
			cityVO.setParentName(parentCity == null ? "" : parentCity.getRegionName());
		}
		return cityVO;
	}

	public List<INode> listNodeLazyVO(List<INode> list) {
		return ForestNodeMerger.merge(list);
	}

}
